package com.heaven1949.tmall.common.web.exception;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * 响应校验数据消息
 *
 * @author zhangjian
 */
@Data
public class ResponseAlertMessage {

    @JsonIgnore
    private static final String DELIMITER_CHAR = ":";
    private String propertyName;
    private String message;

    public ResponseAlertMessage() {
    }

    public ResponseAlertMessage(String message) {
        this.message = message;
    }

    public ResponseAlertMessage(String propertyName, String message) {
        this.propertyName = propertyName;
        this.message = message;
    }

    public static ResponseAlertMessage build(String message) {
        if (StringUtils.isNotBlank(message)) {
            // message: 以分隔符“|”分割字段名和消息
            if (StringUtils.contains(message, DELIMITER_CHAR)) {
                String[] strings = StringUtils.splitByWholeSeparatorPreserveAllTokens(message, DELIMITER_CHAR);
                return new ResponseAlertMessage(strings[0], strings[1]);
            } else {
                return new ResponseAlertMessage(message);
            }
        }
        return null;
    }

}